from web import models
from django.db.models import F
from django.urls import reverse
from django.shortcuts import render, redirect, HttpResponse
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt


@csrf_exempt
def login(request):
    """后台登录"""
    if request.method == 'GET':
        return render(request, 'login.html')
    data = {
        'status': 1000,
        'msg': ''
    }
    username = request.POST.get('username')
    password = request.POST.get('password')
    user_obj = models.AdminInfo.objects.filter(username=username, password=password, is_delete=False,
                                               is_show=True).first()
    if not user_obj:
        # 如果不存在
        data['status'] = 1008
        data['msg'] = '账号密码错误'
        return JsonResponse(data=data)
    data['status'] = 1000
    data['msg'] = '成功'
    request.session['user'] = {'id': user_obj.id, 'username': user_obj.username}
    return JsonResponse(data=data)
